<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Content_management extends Admin_Controller{

	function Content_management(){
        parent::Admin_Controller();
        $CI = & get_instance();    
	}
	
	function index(){
        $CI = & get_instance();
        $CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
		//$CI->load->module_model('block.block_module_list');
		$CI->db->where(BLOCK_FRANCHISE_ID, $auth_data['admin_id']);
		$CI->db->where(BLOCK_SHOW_FLAG, 1);
		$CI->db->order_by(BLOCK_NAME);
		$q = $CI->db->get(BLOCK);
		if ($q->num_rows())
			$this->add('items', $q->result());
		else
			$this->add('items', array());
	
        $CI->db->select('p.'.PAGE_PAGE_ID.', p.'.PAGE_TITLE);
        $CI->db->from(NAVIGATION.' n');
        $CI->db->join(PAGE.' p', 'p.'.PAGE_PAGE_ID.' = n.'.NAVIGATION_PID);
        $CI->db->where('n.'.NAVIGATION_FRANCHISE_ID, $auth_data['admin_id']);
        $CI->db->where('p.'.PAGE_ACTIVE, 1);
        $CI->db->where('p.'.PAGE_FRANCHISE_ID, $auth_data['admin_id']);
        $CI->db->orderby('p.'.PAGE_TITLE);
        $q = $CI->db->get();
        if($q->num_rows() > 0){
            $this->add('pages_combo', $q->result());
        }else{
            $this->add('pages_combo', array());
        }
        
        $this->add('fid', $auth_data['admin_id']);
		$this->module_navigation('Content management');
	}
    /*************** TREE VIEW ********************************/
    function tree_lists(){
        $CI = & get_instance();
        $base_url = 'admin/content_management.content_management.lists';
		$CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
		
		$select = 'SELECT '.NAVIGATION_MENU_ID.', '.NAVIGATION_NAME.', '.NAVIGATION_TOPIC.', '.NAVIGATION_PAR_MENU_ID.', '.NAVIGATION_URL.', '.NAVIGATION_MENU_ID.', '.NAVIGATION_PID;
        $from = ' FROM '.NAVIGATION;
        $order = ' order by '.NAVIGATION.'.'.NAVIGATION_SUB_MENU_ID.' ';
        $cond = ' WHERE '.NAVIGATION_FRANCHISE_ID.' = '.$auth_data['admin_id'];
        $cond .= ' AND ('.NAVIGATION_PAR_MENU_ID.' IS NULL OR '.NAVIGATION_PAR_MENU_ID.' = 0 )';
        
        $sql = $select.$from.$cond.$order;
        $results = $CI->db->query($sql)->result();
        $struct_list = "[
					{
					'id':'nRoot',
					'txt':'Root',
					'editable' : false,
					'onclick' : 'myClick',
                    'openAtLoad' : true,
                    'acceptdrop' : false,
					'items':[";
		$comma = ",";
        for($i=0;$i<count($results);$i++){
			if($i!=0) $struct_list .= $comma;
			$struct_list .= "
						{
						'id':'".$results[$i]->{NAVIGATION_MENU_ID}."_".$results[$i]->{NAVIGATION_PID}."',
						'txt':'".htmlspecialchars($results[$i]->{NAVIGATION_NAME}, ENT_QUOTES)."',
						'title' : 'Double click to rename',
                        'onclick' : 'myClick'
					";

			$this->get_recursive($results[$i]->{NAVIGATION_MENU_ID}, $struct_list, $results[$i]->{NAVIGATION_PID});
			$struct_list .= "}";

			
		}		
			
		/*$select = 'SELECT '.PAGE_PAGE_ID.','.PAGE_TITLE;
        $from = ' FROM '.PAGE;
		$order = ' order by '.PAGE.'.'.PAGE_PAGE_ID.' ';
        $cond = ' WHERE '.PAGE_FRANCHISE_ID.' = '.$auth_data['admin_id'];
		$cond .= ' AND ' . PAGE_SHOW_FLAG .'=1 ';
        $cond .= ' AND ' .PAGE_PAGE_ID.' NOT IN (';
		$cond .= ' SELECT '.NAVIGATION_PID.' FROM '.NAVIGATION;
		$cond .= ' WHERE '.NAVIGATION_FRANCHISE_ID.' = '.$auth_data['admin_id'] .')';
		$sql = $select.$from.$cond.$order;
        $results = $CI->db->query($sql)->result();
		for($i=0;$i<count($results);$i++){*/
			/*$struct_list .= ",
						{
						'id':'page_".$results[$i]->{PAGE_PAGE_ID}."',
						'txt':'".$results[$i]->{PAGE_TITLE}." page',
						'img':'page1.gif',
						'editable' : true,
						'onclick' : 'pageClick',
						'draggable' : false
						}
					";*/
            /*$struct_list .= ",
						{
						'id':'page_".$results[$i]->{PAGE_PAGE_ID}."',
						'txt':'".$results[$i]->{PAGE_TITLE}." page',
						'img':'page1.gif',
						'editable' : true,
						'title' : 'Double click to rename',
                        'acceptdrop' : false,
                        'draggable' : false
						}
					";
		
		}*/
		
		 $struct_list .= "]"; 
		 $struct_list .= "}	];";
		$this->add('struct_list', $struct_list);
		
        $this->view('tree_lists');
    }

	function get_recursive($parent_id, &$str, $pid = 0){
        global $CI;
		$CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
		$CI->load->module_model('content_management.page2_detail_model');
		
        $CI->db->select(NAVIGATION_MENU_ID);
        $CI->db->select(NAVIGATION_NAME);
        $CI->db->select(NAVIGATION_TOPIC);
        $CI->db->select(NAVIGATION_PAR_MENU_ID);
        $CI->db->select(NAVIGATION_URL);
		$CI->db->select(NAVIGATION_MENU_ID);
		$CI->db->select(NAVIGATION_PID);
        $cond = NAVIGATION_FRANCHISE_ID.' = '.$auth_data['admin_id'];
        $cond .= ' AND '.NAVIGATION_PAR_MENU_ID.' = '.$parent_id;

        $CI->db->where($cond);
        $CI->db->orderby(NAVIGATION_SUB_MENU_ID, 'ASC');
        
        $results = $CI->db->get(NAVIGATION)->result();
		$page = $CI->page2_detail_model->gettitle($pid);
		$i = 0;$comma = ",";
        if(!empty($results)){
            $str .= ",'items':[";
            if($pid<>0){
				/*$str .= ",
						{
						'id':'page_".$pid."',
						'txt': '".$page." page',
						'img':'page1.gif',
						'editable' : true,
						'onclick' : 'pageClick',
						'draggable' : false
						}
					";*/
                $str .= "
						{
						'id':'page_".$pid."',
						'txt': '".$page." page',
						'img':'page1.gif',
						'editable' : true,
						'title' : 'Double click to rename',
                        'acceptdrop' : false,
                        'draggable' : false
						},
					";

			}
            foreach($results as $row){    
				if($i!=0) $str .= $comma;  
				$str .= "
						{
						'id':'".$row->{NAVIGATION_MENU_ID}."_".$row->{NAVIGATION_PID}."',
						'txt':'".htmlspecialchars($row->{NAVIGATION_NAME}, ENT_QUOTES)."',
						'title' : 'Double click to rename',
                        'onclick' : 'myClick'
					";
				$this->get_recursive($row->{NAVIGATION_MENU_ID}, $str, $row->{NAVIGATION_PID});
				$str .= "}";
				$i++;
            }
            $str .= "]";
        }
		else{
			if($pid<>0){
				$str .= ",'items':[";
				/*$str .= "
						{
						'id':'page_".$pid."',
						'txt': '".$page." page',
						'img':'page1.gif',
						'editable' : true,
						'onclick' : 'pageClick',
						'draggable' : false
						}
					";*/
                $str .= "
						{
						'id':'page_".$pid."',
						'txt': '".$page." page',
						'img':'page1.gif',
						'title' : 'Double click to rename',
                        'editable' : true,
                        'acceptdrop' : false,
						'draggable' : false
						}
					";
				$str .= "]";
			}
			
		}
    }
	/*************** END TREE ********************************/
	
	/*************** CONTENT PAGE ********************************/
	function get_content(){
        $CI = & get_instance();

		$CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
		$CI->load->library('fckeditor');
        $CI->fckeditor->SetPathOfFranchise();
		$CI->fckeditor->ToolbarSet = 'Gymbaroo_CMS';
        
		$page_id = $CI->input->post('page_id');
		$nav = $CI->input->post('nav');
		if($nav==1){
			$select = 'SELECT p.'.PAGE_VISIBLE.', p.'.PAGE_ACTIVE.', p.'.PAGE_CONTENT_MIDDLE.', p.'.PAGE_META_TITLE.', p.'.PAGE_META_DESCRIPTION.', p.'.PAGE_META_KEYWORD.', p.'.PAGE_CONTENT_LEFT.', p.'.PAGE_CONTENT_RIGHT.', n.'.NAVIGATION_ITEM_VISIBLE;
			$from = ' FROM '.PAGE.' p, '.NAVIGATION.' n ';
			$cond = ' WHERE p.'.PAGE_FRANCHISE_ID.' = '.$auth_data['admin_id'];
			$cond .= ' AND p.'.PAGE_PAGE_ID.'='.$page_id;
			$cond .= ' AND n.'.NAVIGATION_FRANCHISE_ID.' = '.$auth_data['admin_id'];
			$cond .= ' AND n.'.NAVIGATION_PID.'='.$page_id;
		}
		else{
			$select = 'SELECT p.'.PAGE_VISIBLE.', p.'.PAGE_ACTIVE.', p.'.PAGE_CONTENT_MIDDLE.', p.'.PAGE_META_TITLE.', p.'.PAGE_META_DESCRIPTION.', p.'.PAGE_META_KEYWORD.', p.'.PAGE_CONTENT_LEFT.', p.'.PAGE_CONTENT_RIGHT.','.NAVIGATION_ITEM_VISIBLE;
			$from = ' FROM '.PAGE.' p, '.NAVIGATION.' n ';
			$cond = ' WHERE p.'.PAGE_FRANCHISE_ID.' = '.$auth_data['admin_id'];
			$cond .= ' AND p.'.PAGE_PAGE_ID.'='.$page_id;
        }
        $sql = $select.$from.$cond;
        $results = $CI->db->query($sql)->row();
		$this->add('results', $results); 
		
		$CI->fckeditor->Value = $results->{PAGE_CONTENT_MIDDLE};
		$CI->fckeditor->Height = 400;
        $CI->fckeditor->InstanceName = 'middle_content';
        $fck_middle_content = $CI->fckeditor->CreateHtml();
        $this->add('fck_middle_content', $fck_middle_content); 
		
		$CI->fckeditor->Value = $results->{PAGE_CONTENT_LEFT};
        $CI->fckeditor->InstanceName = 'left_content';
        $fck_left_content = $CI->fckeditor->CreateHtml();
        $this->add('fck_left_content', $fck_left_content); 
		
		$CI->fckeditor->Value = $results->{PAGE_CONTENT_RIGHT};
        $CI->fckeditor->InstanceName = 'right_content';
        $fck_right_content = $CI->fckeditor->CreateHtml();
        $this->add('fck_right_content', $fck_right_content); 
		
		$this->add('page_id', $page_id); 
		$this->add('nav', $nav); 
		
        $this->view('content');
    }
	function save_content(){
        $CI = & get_instance();

		$CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
		
		
		if ($CI->input->post('page_id')) {
			$page_id = $CI->input->post('page_id');
			$page_visible = $CI->input->post('page_visible');
			$page_active = $CI->input->post('page_active');
			$page_content_middle = $CI->db->escape_str($CI->input->post('page_content_middle'));
			$page_meta_title = $CI->db->escape_str($CI->input->post('page_meta_title'));
			$page_meta_description = $CI->db->escape_str($CI->input->post('page_meta_description'));
			$page_meta_keyword = $CI->db->escape_str($CI->input->post('page_meta_keyword'));
			$page_content_left = $CI->db->escape_str($CI->input->post('page_content_left'));
			$page_content_right = $CI->db->escape_str($CI->input->post('page_content_right'));
			$item_visible = $CI->input->post('item_visible');
			
			$update = "UPDATE ".NAVIGATION." SET ".NAVIGATION_ITEM_VISIBLE." = ".$item_visible." ";
			$cond = " WHERE ".NAVIGATION_FRANCHISE_ID." = ".$auth_data['admin_id'];
			$cond .= " AND ".NAVIGATION_PID." = ".$page_id;
			$sql = $update.$cond;
			$CI->db->query($sql);
			
			$update = "UPDATE ".PAGE." SET ".PAGE_VISIBLE." = ".$page_visible.", ";
			$update .= PAGE_ACTIVE." = ".$page_active.", ";
			$update .= PAGE_CONTENT_MIDDLE." = '".$page_content_middle."', ";
			$update .= PAGE_META_TITLE." = '".$page_meta_title."', ";
			$update .= PAGE_META_DESCRIPTION." = '".$page_meta_description."', ";
			$update .= PAGE_META_KEYWORD." = '".$page_meta_keyword."', ";
			$update .= PAGE_CONTENT_LEFT." = '".$page_content_left."', ";
			$update .= PAGE_CONTENT_RIGHT." = '".$page_content_right."' ";
			$cond = ' WHERE '.PAGE_FRANCHISE_ID.' = '.$auth_data['admin_id'];
			$cond .= ' AND '.PAGE_PAGE_ID.' = '.$page_id;
			
			$sql = $update.$cond;
			if($CI->db->query($sql))
				echo "<success>Ok</success>";
		}
        
		
		
        $this->view('_blank');
    }
	/*************** END CONTENT PAGE ********************************/
	
	/*************** MODULE LISTS ********************************/
	function module_lists(){
        $CI = & get_instance();

		$CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
		//$CI->load->module_model('block.block_module_list');
		$CI->db->where(BLOCK_FRANCHISE_ID, $auth_data['admin_id']);
		$CI->db->where(BLOCK_SHOW_FLAG, 1);
		$CI->db->order_by(BLOCK_NAME);
		$q = $CI->db->get(BLOCK);
		if ($q->num_rows())
			$this->add('items', $q->result());
		else
			$this->add('items', array());
        $this->view('module_lists');
    }
	/*************** END MODULE LISTS ********************************/
	
  
}